/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.admin.service.impl;


import vip.dexian.admin.dao.LoginLogDao;
import vip.dexian.admin.dto.LoginLogDTO;
import vip.dexian.admin.entity.LoginLog;
import vip.dexian.admin.service.LoginLogService;
import vip.dexian.common.utils.CollectionUtils;
import vip.dexian.core.base.service.BaseServiceImpl;
import vip.dexian.core.exception.ServiceValidationException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 登录日志业务逻辑处理实现
 *
 * @author 挺好的 2023年06月07日 10:48:02
 */
@Service ("loginLogServiceImpl")
@Slf4j
public class LoginLogServiceImpl extends BaseServiceImpl <LoginLog, LoginLogDTO, Long> implements LoginLogService {

    /**
     * 登录日志Dao
     */
    @Resource (name = "loginLogDaoImpl")
    private LoginLogDao loginLogDao;

    /**
     * 保存日志
     *
     * @param entity
     *         实体类
     *
     * @return {@link LoginLog}
     */
    @Override
    @Transactional (rollbackFor = Exception.class)
    @Async
    public void login (LoginLog entity) {
        super.save(entity);
    }

    /**
     * 根据id批量删除登录日志
     *
     * @param ids
     *         id列表
     */
    @Override
    @Transactional (rollbackFor = Exception.class)
    public void delete (Long... ids) {
        log.info("delete parameter:{}", ids);

        this.validDelete(ids);
        List <Long> distinctIds = CollectionUtils.distinct(ids);

        for (Long id : distinctIds) {
            LoginLog adminLoginLog = this.loginLogDao.findByValidationIfNull(id, "登录日志");
            if (Boolean.FALSE.equals(adminLoginLog.getIsAllowedDelete())) {
                throw new ServiceValidationException("A40001");
            }
            super.delete(id);
        }
    }

}
